package com.qianli.user.application;

import com.fqgj.common.api.Response;
import com.qianli.user.domain.enums.UserModelItemStatusEnum;
import com.qianli.user.domain.facatory.UserSocialFactory;
import com.qianli.user.domain.model.UserSocialModel;
import com.qianli.user.domain.model.social.UserContactAddressBook;
import com.qianli.user.domain.model.social.UserContactEmergency;
import com.qianli.user.domain.model.social.UserEmergencyContactor;
import com.qianli.user.ro.social.UserContactAddressBookRO;
import com.qianli.user.ro.social.UserContactEmergencyRO;
import com.qianli.user.ro.social.UserSocialRO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * name:社交信息
 * auth:ting.li@qianli-inc.com
 * date:2018/1/3 14:14
 * version: v1.0.0
 **/
@Service
public class UserSocialApplication extends UserAbstractApplication{

    @Autowired
    private UserSocialFactory userSocialFactory;
    /**
     * 保存通讯录
     * @param userContactAddressBook
     * @return
     */
    public Response<Boolean> saveUserContactAddressBook(UserContactAddressBookRO userContactAddressBook) {
        //加载社交信息
        UserSocialModel oldUm = new UserSocialModel();
        oldUm.setUserCode(userContactAddressBook.getUserCode());
        userSocialFactory.load(oldUm);

        //对比通讯录信息(其实只有新增)
        UserSocialModel newUm = new UserSocialModel();
        UserSocialRO userSocialRO = new UserSocialRO();
        userSocialRO.setUserContactAddressBook(userContactAddressBook);
        convertROToModel(userSocialRO,newUm,oldUm);
        //保存通讯录
        userSocialFactory.store(newUm);
        return Response.ok(true);
    }

    /**
     * 保存紧急联系人
     * @param userContactEmergency
     * @return
     */
    public Response<Boolean> saveUserContactEmergency(UserContactEmergencyRO userContactEmergency) {

        //加载紧急联系人
        UserSocialModel oldUm = new UserSocialModel();
        oldUm.setUserCode(userContactEmergency.getUserCode());
        userSocialFactory.load(oldUm);

        UserSocialModel newUm = new UserSocialModel();
        UserSocialRO userSocialRO = new UserSocialRO();
        userSocialRO.setUserContactEmergency(userContactEmergency);
        convertROToModel(userSocialRO,newUm,oldUm);
        //保存通讯录
        userSocialFactory.store(newUm);
        return Response.ok(true);
    }

    /**
     * ro 转换成 model
     * @param userSocialRO
     * @param newUm
     * @param oldUm
     */
    private void convertROToModel(UserSocialRO userSocialRO, UserSocialModel newUm,
                                  UserSocialModel oldUm) {
        //通讯录
        UserContactAddressBookRO userContactAddressBook = userSocialRO.getUserContactAddressBook();
        if(null != userContactAddressBook){
            UserContactAddressBook ub = new UserContactAddressBook();
            ub.setUserCode(userSocialRO.getUserCode());
            ub.setContactUrl(userContactAddressBook.getContactUrl());
            ub.setItemStatus(UserModelItemStatusEnum.ADD.getType());
            newUm.setUserContactAddressBook(ub);
        }
        //紧急联系人
        UserContactEmergencyRO userContactEmergency = userSocialRO.getUserContactEmergency();
        if(null == userContactEmergency){
            UserContactEmergency ue = new UserContactEmergency();
            ue.setUserCode(userSocialRO.getUserCode());
            //直系联系人
            UserEmergencyContactor family = new UserEmergencyContactor();
            family.setName(userContactEmergency.getFamilyName());
            family.setType(userContactEmergency.getFamilyType());
            family.setContactName(userContactEmergency.getFamilyContactName());
            family.setMobile(userContactEmergency.getFamilyMobile());
            ue.setFamilyContactor(family);
            //朋友联系人
            UserEmergencyContactor friend = new UserEmergencyContactor();
            friend.setName(userContactEmergency.getFriendName());
            friend.setType(userContactEmergency.getFamilyType());
            friend.setContactName(userContactEmergency.getFriendContactName());
            friend.setMobile(userContactEmergency.getFriendMobile());
            ue.setFriendContactor(friend);
            ue.setItemStatus(compare(oldUm.getUserContactEmergency(),userContactEmergency));
            newUm.setUserContactEmergency(ue);
        }
    }

    /**
     * 查询社交信息
     * @param userCode
     * @return
     */
    public Response<UserSocialRO> load(String userCode) {
        //取社交信息
        UserSocialModel socialModel = new UserSocialModel();
        socialModel.setUserCode(userCode);
        userSocialFactory.load(socialModel);
        //
        UserSocialRO socialRO = new UserSocialRO();
        socialRO.setUserCode(userCode);
        //收货地址
        if(null != socialModel.getUserContactAddressBook()){
            UserContactAddressBookRO bookRO = new UserContactAddressBookRO();
            BeanUtils.copyProperties(socialModel.getUserContactAddressBook(),bookRO);
            socialRO.setUserContactAddressBook(bookRO);
        }
        //紧急联系人
        if(null != socialModel.getUserContactEmergency()){
            UserContactEmergencyRO emergency = new UserContactEmergencyRO();
            BeanUtils.copyProperties(socialModel.getUserContactEmergency(),emergency);
            socialRO.setUserContactEmergency(emergency);
        }
        return Response.ok(socialRO);
    }
}
